﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace FileSharingHosting
{
    /// <summary>
    /// Сводное описание для FileHandler
    /// </summary>
    public class FileHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            var file_key = context.Request.Params["id"];    // short link
            if (file_key != null)
            {
                var fbll = new Business.FileBusinessLogic();
                var hfile = fbll.LoadHostedFile(fbll.GetFileID(file_key));  // loading hosted file info from db
                if (hfile.ID != -1)
                {
                    FileInfo hfile_info = new FileInfo(hfile.ServerSideFileName);
                    long last_Length = 0;
                    if (hfile_info.Exists)
                    {
                        using (var file_stream = new FileStream(hfile_info.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            try
                            {
                                last_Length = file_stream.Length;
                                context.Response.ContentType = "application/octet-stream";
                                context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", hfile.OriginalFileName));
                                context.Response.AppendHeader("Content-Length", last_Length.ToString());
                                var buffer = new byte[1024];

                                while (last_Length > 0)
                                {
                                    if (context.Response.IsClientConnected)
                                    {
                                        int read_length = file_stream.Read(buffer, 0, 1024);
                                        context.Response.OutputStream.Write(buffer, 0, read_length);
                                        context.Response.Flush();
                                        last_Length -= read_length;
                                    }
                                    else
                                    {
                                        // Client disconnected
                                        last_Length = -1;
                                    }
                                }
                            }
                            catch (IOException e)
                            {
                                // some error encounted while transfering data
                            }
                        }
                    }
                    else
                    {
                        // file not found on disk
                    }
                    context.Response.Flush();
                    context.Response.End();
                }
                else
                {
                    // file not found in db
                }
            }



        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}